#include <stdio.h>
#include <string.h>

#define LEN 1000001

int next[LEN];
int len;
char str[LEN];

void getNext()
{
int i, j;
next[0] = -1;
i = 0;
j = -1;
while(str[i] != '\0') {
if(-1 == j || str[i] == str[j]) {
++i;
++j;
next[i] = j;
} else {
j = next[j];
}
}
}

int main()
{
int files;

scanf("%d", &files);

while(files--) {
int i;

scanf("%s", str);
getNext();

int len = strlen(str);

for(i = len - 1 - next[len]; i >= next[len]; i--) {
if(next[i+1] == next[len]) {
printf("%d\n", next[i+1]);
break;
}
}

if(next[len] == len - 1) {
printf("%d\n", len / 3);
} else if (i < next[len]) {
printf("0\n");
}
}
return 0;
}
